<?php

require_once("database.php");
require_once("exception.php");

class Commodity
{
	private $id;
	private $typeId;
	private $name;
	private $description;
	private $price;
	private $picURL;
	private $discount;
	private $sale;
	
	function __construct($id = 0,
						 $typeId = 0,
						 $name = "",
						 $description = "",
						 $price = 0,
						 $picURL = "blank.png",
						 $discount = 1,
						 $sale = 0)
	{
		$this->id = $id;
		$this->typeId = $typeId;
		$this->name = $name;
		$this->description = $description;
		$this->price = $price;
		$this->picURL = $picURL;
		$this->discount = $discount;
		$this->sale = $sale;
	}
	
	public function setId($id) {$this->id = $id;}
	public function setTypeId($typeId) {$this->typeId = $typeId;}
	public function setName($name) {$this->name = $name;}
	public function setDescription($description) {$this->description = $description;}
	public function setPrice($price) {$this->price = $price;}
	public function setPicURL($picURL) {$this->picURL = $picURL;}
	public function setDiscount($discount) {$this->discount = $discount;}
	public function setSale($sale) {$this->sale = $sale;}
	
	public function getId() {return $this->id;}
	public function getTypeId() {return $this->typeId;}
	public function getName() {return $this->name;}
	public function getDescription() {return $this->description;}
	public function getPrice() {return $this->price;}
	public function getPicURL() {return $this->picURL;}
	public function getDiscount() {return $this->discount;}
	public function getSale() {return $this->sale;}
}



class CommodityDAO
{
	private $db;
	function __construct()
	{
		$this->db = new Database();
	}
	
	public function addCommodity($commodity)
	{
		$conn = 0;
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("insert into ESCommodity(typeId, name, description, price, picURL, discount, sale) values(?, ?, ?, ?, ?, ?, 0)", array($commodity->getTypeId(), $commodity->getName(), $commodity->getDescription(), $commodity->getPrice(), $commodity->getPicURL(), $commodity->getDiscount()));
			$prepStmt->executeUpdate();
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
	}
	
	public function getCommodityById($id)
	{
		$conn = 0;
		$commodity = new Commodity();
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("select * from ESCommodity where id=?", array($id));
			$rs = $prepStmt->executeQuery();
			if($rs->next())
			{
				$commodity->setId($rs->value("id"));
				$commodity->setTypeId($rs->value("typeId"));
				$commodity->setName($rs->value("name"));
				$commodity->setDescription($rs->value("description"));
				$commodity->setPrice($rs->value("price"));
				$commodity->setPicURL($rs->value("picURL"));
				$commodity->setDiscount($rs->value("discount"));
				$commodity->setSale($rs->value("sale"));
			}
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $commodity;
	}
	
	public function getCommodityListByCategoryId($categoryId)
	{
		$conn = 0;
		$list = array();
		try
		{
			$conn = $this->db->getConnection();
			$prepStmt = $conn->prepareStatement("exec spGetCommodityListByCategoryId ?", array($categoryId));
			$rs = $prepStmt->executeQuery();
			$rs->next();
			while($rs->next())
			{
				$id = $rs->value("id");
				$typeId = $rs->value("typeId");
				$name = $rs->value("name");
				$description = $rs->value("description");
				$price = $rs->value("price");
				$picURL = $rs->value("picURL");
				$discount = $rs->value("discount");
				$sale = $rs->value("sale");
				$commodity = new Commodity($id, $typeId, $name,
										   $description, $price,
										   $picURL, $discount, $sale);
				$list[$id] = $commodity;
			}
			$this->db->close($conn);
		}
		catch(SQLException $e)
		{
			$this->db->close($conn);
			print($e->getMessage());
		}
		
		return $list;
	}
}

?>